package com.highgain.displacementtrend.controller;

import com.highgain.displacementtrend.entity.Device;
import com.highgain.displacementtrend.entity.Result;
import com.highgain.displacementtrend.service.DeviceService;
import com.highgain.displacementtrend.util.Flexigrid;
import com.highgain.displacementtrend.util.PageView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanglifeng
 * @date 2021/04/27 17:33
 **/
@Controller
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @GetMapping("/device/page")
    public String devicePage(){
        return "device/list";
    }

    @PostMapping("/device/queryAll")
    @ResponseBody
    public Result queryAllDevice(Device device){
        return deviceService.queryAllDevice(device);
    }

    @PostMapping("/device/query")
    @ResponseBody
    public Flexigrid queryDevice(Device device, Flexigrid flex, HttpServletRequest request){
        String rows = request.getParameter("rows");
        PageView<Device> pageView = new PageView<>(flex.getPage());
        pageView.setPageSize(Integer.parseInt(rows));
        pageView = deviceService.queryDevice(pageView, device);
        flex.setTotal(pageView.getRowCount());
        flex.setRows(pageView.getRecords());
        return flex;
    }
}
